package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// Role 角色
type Role struct {
	Id         int64
	RoleName   string
	RoleNo     string
	CreateTime time.Time `orm:"auto_now_add;null"`
	UpdateTime time.Time `orm:"auto_now;null"`
	IsDelete   bool      `orm:"default(0)" json:"-"`
}

func (Role) TableName() string {
	return "t_role"
}

func NewRole() *Role {
	return &Role{}
}

// Save 保存
func (role *Role) Save() (int64, error) {
	o := orm.NewOrm()
	return o.Insert(role)
}

// Update 更新指定字段
func (role *Role) Update(cols ...string) (int64, error) {
	o := orm.NewOrm()
	return o.Update(role, cols...)
}

// Delete 删除
func (role *Role) Delete() (int64, error) {
	o := orm.NewOrm()
	return o.Delete(role)
}

// DelLogic 逻辑删除
func (role *Role) DelLogic() error {
	if role.Id < 1 {
		return nil
	}
	o := orm.NewOrm()
	_, err := o.QueryTable(role).Filter(IdColumnName, role.Id).Update(orm.Params{
		"is_delete": 1,
	})
	return err
}

// GetById 根据id获取
func (role *Role) GetById() error {
	o := orm.NewOrm()
	return o.Read(role)
}

func (role *Role) GetByRoleNo() error {
	o := orm.NewOrm()
	return o.QueryTable(role).Filter("role_no", role.RoleNo).One(role)
}
